<!-- 学生锻炼编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改学生锻炼':'添加学生锻炼'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
      
      <el-form-item label="用户头像:">
        <uploadImage :limit="1" v-model="form.avatar"></uploadImage>
      </el-form-item>
                        
      <el-form-item
        label="用户唯一标识:"
        prop="openid">
        <el-input
          :maxlength="20"
          v-model="form.openid"
          placeholder="请输入用户唯一标识"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="用户名:"
        prop="username">
        <el-input
          :maxlength="20"
          v-model="form.username"
          placeholder="请输入用户名"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="密码:"
        prop="password">
        <el-input
          :maxlength="20"
          v-model="form.password"
          placeholder="请输入密码"
          clearable/>
      </el-form-item>
                              
      <el-form-item label="所属组织:" prop="organization">
        <el-input-number
          :min="0"
          v-model="form.organization"
          placeholder="请输入所属组织"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item label="所属院系:" prop="superior">
        <el-input-number
          :min="0"
          v-model="form.superior"
          placeholder="请输入所属院系"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item label="所属年级:" prop="grade">
        <el-input-number
          :min="0"
          v-model="form.grade"
          placeholder="请输入所属年级"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item label="所属班级:" prop="class">
        <el-input-number
          :min="0"
          v-model="form.class"
          placeholder="请输入所属班级"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item label="所属专业:" prop="professional">
        <el-input-number
          :min="0"
          v-model="form.professional"
          placeholder="请输入所属专业"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item
        label="体育系班:"
        prop="features">
        <el-input
          :maxlength="20"
          v-model="form.features"
          placeholder="请输入体育系班"
          clearable/>
      </el-form-item>
                      
      <el-form-item label="账号类型:" prop="type">
        <el-select
          clearable
          class="ele-block"
          v-model="form.type"
          placeholder="请选择账号类型">
                  <el-option label="=学生" :value="1"/>
                </el-select>
      </el-form-item>
                              
      <el-form-item
        label="学籍号:"
        prop="roll">
        <el-input
          :maxlength="20"
          v-model="form.roll"
          placeholder="请输入学籍号"
          clearable/>
      </el-form-item>
                              
      <el-form-item label="会员等级:" prop="member_level">
        <el-input-number
          :min="0"
          v-model="form.member_level"
          placeholder="请输入会员等级"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item
        label="真实姓名:"
        prop="realname">
        <el-input
          :maxlength="20"
          v-model="form.realname"
          placeholder="请输入真实姓名"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="用户昵称:"
        prop="nickname">
        <el-input
          :maxlength="20"
          v-model="form.nickname"
          placeholder="请输入用户昵称"
          clearable/>
      </el-form-item>
                              
      <el-form-item label="性别（1男 2女 3未知）:" prop="gender">
        <el-input-number
          :min="0"
          v-model="form.gender"
          placeholder="请输入性别（1男 2女 3未知）"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item
        label="身份证号:"
        prop="idcard">
        <el-input
          :maxlength="20"
          v-model="form.idcard"
          placeholder="请输入身份证号"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="手机号:"
        prop="mobile">
        <el-input
          :maxlength="20"
          v-model="form.mobile"
          placeholder="请输入手机号"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="出生日期:"
        prop="birthday">
        <el-input
          :maxlength="20"
          v-model="form.birthday"
          placeholder="请输入出生日期"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="户籍省份编号:"
        prop="province_code">
        <el-input
          :maxlength="20"
          v-model="form.province_code"
          placeholder="请输入户籍省份编号"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="户籍城市编号:"
        prop="city_code">
        <el-input
          :maxlength="20"
          v-model="form.city_code"
          placeholder="请输入户籍城市编号"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="户籍区/县编号:"
        prop="district_code">
        <el-input
          :maxlength="20"
          v-model="form.district_code"
          placeholder="请输入户籍区/县编号"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="详细地址:"
        prop="address">
        <el-input
          :maxlength="20"
          v-model="form.address"
          placeholder="请输入详细地址"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="个性签名:"
        prop="signature">
        <el-input
          :maxlength="20"
          v-model="form.signature"
          placeholder="请输入个性签名"
          clearable/>
      </el-form-item>
                      
      <el-form-item label="设备类型:" prop="device">
        <el-select
          clearable
          class="ele-block"
          v-model="form.device"
          placeholder="请选择设备类型">
                  <el-option label="苹果" :value="1"/>
                  <el-option label="安卓" :value="2"/>
                  <el-option label="WAP站" :value="3"/>
                  <el-option label="PC站" :value="4"/>
                  <el-option label="后台添加" :value="5"/>
                </el-select>
      </el-form-item>
                              
      <el-form-item
        label="推送的别名:"
        prop="device_code">
        <el-input
          :maxlength="20"
          v-model="form.device_code"
          placeholder="请输入推送的别名"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="推送的别名:"
        prop="push_alias">
        <el-input
          :maxlength="20"
          v-model="form.push_alias"
          placeholder="请输入推送的别名"
          clearable/>
      </el-form-item>
                      
      <el-form-item label="来源:" prop="source">
        <el-select
          clearable
          class="ele-block"
          v-model="form.source"
          placeholder="请选择来源">
                  <el-option label="1、APP注册；2、后台添加；" :value="12"/>
                </el-select>
      </el-form-item>
                              
      <el-form-item label="是否启用 1、启用  2、停用:" prop="status">
        <el-input-number
          :min="0"
          v-model="form.status"
          placeholder="请输入是否启用 1、启用  2、停用"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item
        label="客户端版本号:"
        prop="app_version">
        <el-input
          :maxlength="20"
          v-model="form.app_version"
          placeholder="请输入客户端版本号"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="我的推广码:"
        prop="code">
        <el-input
          :maxlength="20"
          v-model="form.code"
          placeholder="请输入我的推广码"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="最近登录IP:"
        prop="login_ip">
        <el-input
          :maxlength="20"
          v-model="form.login_ip"
          placeholder="请输入最近登录IP"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="登录时间:"
        prop="login_time">
        <el-input
          :maxlength="20"
          v-model="form.login_time"
          placeholder="请输入登录时间"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="上次登录地点:"
        prop="login_region">
        <el-input
          :maxlength="20"
          v-model="form.login_region"
          placeholder="请输入上次登录地点"
          clearable/>
      </el-form-item>
                              
      <el-form-item label="登录总次数:" prop="login_count">
        <el-input-number
          :min="0"
          v-model="form.login_count"
          placeholder="请输入登录总次数"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                
      <el-form-item label="个人简介:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.intro"
          placeholder="请输入个人简介"/>
      </el-form-item>
          </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'MembEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              avatar: [
          {required: true, message: '请输入用户头像', trigger: 'blur'}
        ],
                  openid: [
          {required: true, message: '请输入用户唯一标识', trigger: 'blur'}
        ],
                  username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
                  password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
                  organization: [
          {required: true, message: '请输入所属组织', trigger: 'blur'}
        ],
                  superior: [
          {required: true, message: '请输入所属院系', trigger: 'blur'}
        ],
                  grade: [
          {required: true, message: '请输入所属年级', trigger: 'blur'}
        ],
                  class: [
          {required: true, message: '请输入所属班级', trigger: 'blur'}
        ],
                  professional: [
          {required: true, message: '请输入所属专业', trigger: 'blur'}
        ],
                  features: [
          {required: true, message: '请输入体育系班', trigger: 'blur'}
        ],
                  
        type: [
          {required: true, message: '请选择账号类型', trigger: 'blur'}
        ],
                          roll: [
          {required: true, message: '请输入学籍号', trigger: 'blur'}
        ],
                  member_level: [
          {required: true, message: '请输入会员等级', trigger: 'blur'}
        ],
                  realname: [
          {required: true, message: '请输入真实姓名', trigger: 'blur'}
        ],
                  nickname: [
          {required: true, message: '请输入用户昵称', trigger: 'blur'}
        ],
                  gender: [
          {required: true, message: '请输入性别（1男 2女 3未知）', trigger: 'blur'}
        ],
                  idcard: [
          {required: true, message: '请输入身份证号', trigger: 'blur'}
        ],
                  mobile: [
          {required: true, message: '请输入手机号', trigger: 'blur'}
        ],
                  birthday: [
          {required: true, message: '请输入出生日期', trigger: 'blur'}
        ],
                  province_code: [
          {required: true, message: '请输入户籍省份编号', trigger: 'blur'}
        ],
                  city_code: [
          {required: true, message: '请输入户籍城市编号', trigger: 'blur'}
        ],
                  district_code: [
          {required: true, message: '请输入户籍区/县编号', trigger: 'blur'}
        ],
                  address: [
          {required: true, message: '请输入详细地址', trigger: 'blur'}
        ],
                  signature: [
          {required: true, message: '请输入个性签名', trigger: 'blur'}
        ],
                  
        device: [
          {required: true, message: '请选择设备类型', trigger: 'blur'}
        ],
                          device_code: [
          {required: true, message: '请输入推送的别名', trigger: 'blur'}
        ],
                  push_alias: [
          {required: true, message: '请输入推送的别名', trigger: 'blur'}
        ],
                  
        source: [
          {required: true, message: '请选择来源', trigger: 'blur'}
        ],
                          status: [
          {required: true, message: '请输入是否启用 1、启用  2、停用', trigger: 'blur'}
        ],
                  app_version: [
          {required: true, message: '请输入客户端版本号', trigger: 'blur'}
        ],
                  code: [
          {required: true, message: '请输入我的推广码', trigger: 'blur'}
        ],
                  login_ip: [
          {required: true, message: '请输入最近登录IP', trigger: 'blur'}
        ],
                  login_time: [
          {required: true, message: '请输入登录时间', trigger: 'blur'}
        ],
                  login_region: [
          {required: true, message: '请输入上次登录地点', trigger: 'blur'}
        ],
                  login_count: [
          {required: true, message: '请输入登录总次数', trigger: 'blur'}
        ],
                  intro: [
          {required: true, message: '请输入个人简介', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/memb/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
